﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Shrestha.Charts.Graph
{
    /// <summary>
    /// Interaction logic for TriGraphFilter.xaml
    /// </summary>
    public partial class TriGraphFilter : Window
    {
        private IViewModel WindowViewModel;
        private string filterString;

        public TriGraphFilter()
        {
            InitializeComponent();
        }

        public TriGraphFilter(IViewModel chartWindowViewModel)
        {
            InitializeComponent();
            this.WindowViewModel = chartWindowViewModel;
            this.latFrom.Text = chartWindowViewModel.GetRunner().Meta.MinLatitude.ToString();
            this.latTo.Text = chartWindowViewModel.GetRunner().Meta.MaxLatitude.ToString();

            this.lngFrom.Text = chartWindowViewModel.GetRunner().Meta.MinLongitude.ToString();
            this.lngTo.Text = chartWindowViewModel.GetRunner().Meta.MaxLongitude.ToString();

            this.dateFrom.SelectedDate = chartWindowViewModel.GetRunner().Meta.MinEventDate;
            this.dateTo.SelectedDate = chartWindowViewModel.GetRunner().Meta.MaxEventDate;
        }



        internal string GetFilterString()
        {
            return filterString;
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonFilter_Click(object sender, RoutedEventArgs e)
        {
            filterString = "";
            if (this.checkBoxLatitude.IsChecked.Value)
                filterString += WindowViewModel.GetRunner().LatName + " > " + latFrom.Text + " and " + WindowViewModel.GetRunner().LatName + " < " + latTo.Text;
            if (this.checkBoxLongitude.IsChecked.Value)
            {
                if (!filterString.Equals("")) filterString += " and ";
                filterString += WindowViewModel.GetRunner().LngName + " > " + lngFrom.Text + " and " + WindowViewModel.GetRunner().LngName + " < " + lngTo.Text;
            }
            if (this.checkBoxDate.IsChecked.Value)
            {
                if (!filterString.Equals("")) filterString += " and ";
                filterString += WindowViewModel.GetRunner().DateTimeName + " > '" + dateFrom.SelectedDate.Value + "' and " + WindowViewModel.GetRunner().DateTimeName + " < '" + dateTo.SelectedDate.Value+"'";
            }
            this.Close();
        }

        private void dateFrom_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            dateTo.DisplayDateStart = dateFrom.SelectedDate;
        }
    }
}
